package vn.orderonline.com.core.business.restaurant;

import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mysql.jdbc.StringUtils;

import vn.orderonline.com.core.mvc.model.RestaurantDto;
import vn.orderonline.core.commons.utils.DateUtils;
import vn.orderonline.core.dao.RestaurantDao;
import vn.orderonline.core.dao.entity.Restaurant;

@Service
@Transactional
public class RestaurantManagerImpl implements IRestaurantManager {

	@Autowired
	private RestaurantDao restaurantDao;
	
	@Override
	public List<RestaurantDto> search(
			RestaurantDto objSearch) {
		RestaurantDto result = null;
		List<RestaurantDto> listResult = new ArrayList<>();
		List<Restaurant> list = restaurantDao.search(objSearch.getRestaurantName(), 
				objSearch.getRestaurantTitle(), objSearch.getRestaurantAddress(), 
				objSearch.getOpenTime(), objSearch.getCloseTime(), objSearch.getDeliveryPrice(), 
				objSearch.getMinOrder());
		if (!list.isEmpty()) {
			for (Restaurant res : list) {
				result = new RestaurantDto();
				result.setRestaurantId(res.getRestaurantId());
				result.setRestaurantName(res.getRestaurantName());
				result.setRestaurantTitle(res.getRestaurantTitle());
				result.setRestaurantAddress(res.getRestaurantAddress());
				result.setOpenTime(String.valueOf(res.getOpenTime()));
				result.setCloseTime(String.valueOf(res.getCloseTime()));
				result.setDeliveryPrice(res.getDeliveryPrice());
				result.setMinOrder(res.getMinOrder());
				listResult.add(result);
			}
		}
		return listResult;
	}

	@Override
	public List<RestaurantDto> getAllRestaurants() {
		List<Restaurant> list = restaurantDao.findAll();
		List<RestaurantDto> result = new ArrayList<>();
		RestaurantDto obj = null;
		for (Restaurant res : list)  {
			obj = new RestaurantDto();
			obj.setRestaurantId(res.getRestaurantId());
			obj.setRestaurantName(res.getRestaurantName());
			obj.setRestaurantTitle(res.getRestaurantTitle());
			obj.setRestaurantAddress(res.getRestaurantAddress());
			obj.setOpenTime(String.valueOf(res.getOpenTime()));
			obj.setCloseTime(String.valueOf(res.getCloseTime()));
			obj.setDeliveryPrice(res.getDeliveryPrice());
			obj.setMinOrder(res.getMinOrder());
			obj.setCreateId(res.getCreateId());
			obj.setUpdateId(res.getUpdateId());
			result.add(obj);
		}
		return result;
	}

	@Override
	public RestaurantDto getRestaurantById(Integer id) {
		Restaurant res = restaurantDao.find(id);
		RestaurantDto dto = new RestaurantDto();
		if (res != null) {
			dto.setRestaurantId(res.getRestaurantId());
			dto.setRestaurantName(res.getRestaurantName());
			dto.setRestaurantTitle(res.getRestaurantTitle());
			dto.setRestaurantAddress(res.getRestaurantAddress());
			dto.setRestaurantImage(res.getRestaurantImage());
			dto.setOpenTime(String.valueOf(res.getOpenTime()));
			dto.setCloseTime(String.valueOf(res.getCloseTime()));
			dto.setDeliveryPrice(res.getDeliveryPrice());
			dto.setMinOrder(res.getMinOrder());
			dto.setRestaurantStatus(res.getRestaurantStatus());
			dto.setRestaurantNote(res.getRestaurantNote());
			dto.setPaymentMethod(res.getPaymentMethod());
		}
		return dto;
	}
	
	@Override
	public void add(RestaurantDto res) {
		Restaurant rest = new Restaurant();
		rest.setRestaurantName(res.getRestaurantName());
		rest.setRestaurantTitle(res.getRestaurantTitle());
		rest.setRestaurantAddress(res.getRestaurantAddress());
		rest.setRestaurantImage(res.getRestaurantImage());
		rest.setOpenTime(Time.valueOf(res.getOpenTime() + ":00"));
		rest.setCloseTime(Time.valueOf(res.getCloseTime() + ":00"));
		rest.setDeliveryPrice(res.getDeliveryPrice());
		rest.setMinOrder(res.getMinOrder());
		rest.setCreateTime(new Date(DateUtils.now()));
		rest.setCreateId("zuchihashshui");
		rest.setRestaurantStatus(1);
		rest.setRestaurantNote(res.getRestaurantNote());
		rest.setPaymentMethod(1);
		restaurantDao.save(rest);
	}

	@Override
	public Boolean checkExistedRestaurant(String restaurantName) { 
		if (!StringUtils.isNullOrEmpty(restaurantName)) {
			return restaurantDao.checkExistedRestaurant(restaurantName.trim());
		}
		return false;
	}

	@Override
	public void delete(Integer id) {
		Restaurant res = restaurantDao.find(id);
		if (id != null) {
			restaurantDao.delete(res);
		}
	}

	@Override
	public Boolean update(RestaurantDto res) {
		if (res != null) {
			Restaurant rest = new Restaurant();
			rest.setRestaurantId(res.getRestaurantId());
			rest.setRestaurantTitle(res.getRestaurantTitle());
			rest.setRestaurantAddress(res.getRestaurantAddress());
			rest.setRestaurantName(res.getRestaurantName());
			rest.setRestaurantImage(res.getRestaurantImage());
			rest.setOpenTime(Time.valueOf(res.getOpenTime()));
			rest.setCloseTime(Time.valueOf(res.getCloseTime()));
			rest.setDeliveryPrice(res.getDeliveryPrice());
			rest.setMinOrder(res.getMinOrder());
			rest.setCreateTime(new Date(DateUtils.now()));
			rest.setUpdateTime(new Date(DateUtils.now()));
			rest.setCreateId("zuchihashishui");
			rest.setUpdateId("zuchhashishui1");
			rest.setRestaurantStatus(1);
			rest.setRestaurantNote(res.getRestaurantNote());
			rest.setPaymentMethod(1);
			restaurantDao.update(rest);
			return true;
		}
		return false;
	}
	
}
